/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hongyan.page;

import com.hongyan.util.JDBCUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class BaseDAO {

    public int excuteUpdate(String sql) throws Exception {
        Statement st = null;
        try {
            st = JDBCUtils.getConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            return st.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
            javax.swing.JOptionPane.showMessageDialog(null, e, "错误", javax.swing.JOptionPane.ERROR_MESSAGE);
            throw e;

        } finally {
            try {

                if (st != null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public List<Object[]> excuteQuery(String sql) {
        Statement st = null;
        ResultSet rs = null;
        List list = new ArrayList();
        try {
            st = JDBCUtils.getConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            rs = st.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            while (rs.next()) {
                Object[] entityArr = new Object[columnCount];
                for (int i = 0; i < columnCount; i++) {
                    entityArr[i] = rs.getObject(i + 1);
                }

                list.add(entityArr);
            }
            System.out.println(list.size());
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            javax.swing.JOptionPane.showMessageDialog(null, e, "错误", javax.swing.JOptionPane.ERROR_MESSAGE);
            return null;
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void excuteQuery2(String sql) {
        Statement st = null;
        ResultSet rs = null;
        List list = new ArrayList();
        try {
            st = JDBCUtils.getConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            rs = st.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            while (rs.next()) {
                Object[] entityArr = new Object[columnCount];
                for (int i = 0; i < columnCount; i++) {
                    entityArr[i] = rs.getObject(i + 1);
                }
                addEntiry(entityArr);

            }
            System.out.println(list.size());

        } catch (Exception e) {
            e.printStackTrace();
            javax.swing.JOptionPane.showMessageDialog(null, e, "错误", javax.swing.JOptionPane.ERROR_MESSAGE);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public int count(String coutSql) {
        Statement st = null;
        ResultSet rs = null;
        List list = new ArrayList();
        try {
            st = JDBCUtils.getConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            rs = st.executeQuery(coutSql);
            if (rs.next()) {
                return rs.getInt(1);
            }
            return 0;
        } catch (Exception e) {
            System.out.println("coutSql=" + coutSql);
            e.printStackTrace();
            javax.swing.JOptionPane.showMessageDialog(null, e, "错误", javax.swing.JOptionPane.ERROR_MESSAGE);
            return 0;
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (Exception e) {
                }
            }

        }
    }

    public void addEntiry(Object[] entityArr) {
    }
    private static BaseDAO instance;

    public static BaseDAO getInstance() {
        if (instance == null) {
            instance = new BaseDAO();
        }
        return instance;
    }
}
